{% extends "base.html" %}
{% block content %}
<script src="/inc/waf.js"></script>
{% if manage_rules == '1' %}
	{% from 'include/input_macros.html' import input, checkbox %}
	<table class="overview" id="waf_rules">
		<tr class="overviewHead">
			<td class="padding10 first-collumn">
				Rule name
			</td>
			<td style="width: 100px;">Enabled</td>
			<td>Description</td>
			<td></td>
		</tr>
		{% for r in rules %}
		<tr class="{{ loop.cycle('odd', 'even') }}" id="rule-{{r.id}}">
			<td class="padding10 first-collumn">{{r.rule_name}}</td>
			<td class="checkbox">
				{% set id = 'rule_id-' + r.id|string() %}
				{% if r.en == 1 %}
					{{ checkbox(id, checked='checked') }}
				{% else %}
					{{ checkbox(id) }}
				{% endif %}
			</td>
			<td style="padding-top: 5px;padding-bottom: 10px;">{{r.desc}}</td>
			<td style="padding: 0 10px 0 10px;">
				<a href="waf.py?waf_rule_id={{r.id}}&serv={{serv}}" class="ui-button ui-widget ui-corner-all" title="View this rule versions">View</a>
			</td>
		</tr>
		{% endfor %}
	</table>
{% elif waf_rule_file %}
	<center>
		<link rel="stylesheet" href="/inc/codemirror/codemirror.css">
		<script src="/inc/codemirror/codemirror.js"></script>
		<script src="/inc/codemirror/modsec.js"></script>
			<h4>Config {{waf_rule_file}} from {{ serv }}</h4>
			</center>
			<form action="waf.py" name="saveconfig" id="saveconfig" method="post">
				<input type="hidden" value="{{ serv }}" name="serv">
				<input type="hidden" value="{{ cfg }}.old" name="oldconfig">
				<input type="hidden" value="{{ token }}" name="token">
				<input type="hidden" value="{{ service }}" name="service">
				<div style="margin-left: 20%;width: 60%;">
					<textarea name="config" id="config_text_area" class="config" rows="35" cols="100">{{ config }}</textarea>
				</div>
				<p>
					<center>
						<a href="waf.py?manage_rules=1&serv={{serv}}" class="ui-button ui-widget ui-corner-all" title="Return to rules management">Back</a>
					</center>
				</p>
			</form>
		<style>
		.CodeMirror {
			line-height: 1.2em;
			height: 70%;
		}
		</style>
		<script>
			var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("config_text_area"),
				{mode: "modsec", lineNumbers: true, autocapitalize: true, autocorrect: true, spellcheck: true, readOnly: true});
			myCodeMirror.refresh();
		</script>
{% else %}
	<style>
	.alert-danger {
		width: 450px;
		display: inline-block;
		height: 25px;
		padding-top: 0px;
	}
	</style>
	<script>
		$("#secIntervals").css("display", "none");
		var ip = []
		var hostnamea = []
		{% for s in servers_all %}
		{% if s.15|int() == 1 %}
			ip.push("{{s[2]}}");
			var host = "{{s[2]}}";
			host = host.replace(/\./g, '\\.');
			hostnamea.push(host);
		{% endif %}
		{% endfor %}

	</script>
	{% if servers_all|length == 0 %}
		{% include 'include/getstarted.html' %}
	{% else %}
	<table class="overview">
		<tr class="overviewHead">
			<td class="padding10 first-collumn">Server</td>
			<td class="padding10">Actions</td>
			<td class="padding10">WAF mode</td>
			<td>Metrics</td>
			<td>Manage rules</td>
			<td>Log</td>
			<td>
				<a onclick="showOverviewWaf(ip, hostnamea)" title="Refresh" style="float: right; margin-right: 25px;">
					<span class="refresh"></span>
				</a>
			</td>
		</tr>
		{% for s in servers_all %}
			<tr class="{{ loop.cycle('odd', 'even') }}" id="{{s[2]}}"></tr>
		{% endfor %}
	</table>

	<link href="/inc/chart.min.css" rel="stylesheet">
	<script src="/inc/overview.js"></script>
	<script src="/inc/metrics.js"></script>
	<script src="/inc/chart.min.js"></script>
	<div style="padding-left: 25px;float: left;margin-top: 3px;">
		<b>Time range:</b>
		<select title="Choose time range" id="time-range">
			<option value="30">30 minutes</option>
			<option value="60">1 hour</option>
			<option value="180">3 hours</option>
			<option value="360">6 hours</option>
			<option value="720">12 hours</option>
		</select>
	</div>
	<div id="refresh" style="text-align: right;margin-top: 10px;margin-right: 20px; margin-bottom: 10px;" title="Refresh metrics" onclick="showWafMetrics()">
		<span class="refresh"></span>
	</div>
	{% for s in servers %}
	<div class="chart-container">
		<canvas id="s_{{s.ip}}" role="img"></canvas>
	</div>
	{% endfor %}
	<div id="dialog-confirm" style="display: none;">
	  <p><span class="ui-icon ui-icon-alert" style="float:left; margin:3px 12px 20px 0;"></span>Are you sure?</p>
	</div>
	<script>
		function showWafMetrics() {
			{% for s in servers %}
				getWafChartData('{{s.ip}}');
			{% endfor %}
		}
		showWafMetrics();
		showOverviewWaf(ip, hostnamea);
		$( function() {
			$("#time-range").on('selectmenuchange', function () {
				removeData()
				let metrics = new Promise(
					(resolve, reject) => {
						{% for s in servers %}
							getWafChartData('{{s.ip}}')
						{% endfor %}
					});
				metrics.then();
			});
		});
	</script>
	<div class="add-note addName alert-info" style="width: inherit; margin-right: 15px; clear: both;">
		Read the description and watch a video about WAF <a href="https://roxy-wi.org/description.py?description=waf" class="link" title="WAF" target="_blank">here</a>
	</div>
	{% endif %}
{% endif %}
{% endblock %}